/**
 * Created with JetBrains WebStorm.
 * User: Администратор
 * Date: 9/13/13
 * Time: 10:18 PM
 * To change this template use File | Settings | File Templates.
 */
/**
 * Created with JetBrains WebStorm.
 * User: Администратор
 * Date: 9/12/13
 * Time: 8:39 PM
 * To change this template use File | Settings | File Templates.
 */
function CanvasMarker() {
    this.cnv = null;
    this.cnvSh = null;
    var colorPlus, colorMinus, rgba1, rgba2, op;

    this.init = function () {
        this.cnv = [];
        this.cnvSh = [];
    };


    this.open = function (countMarker, speed, azimut, noSpid, signal) {
        this.init();
        this.setCanvas(countMarker,speed,azimut);

    };

    this.setCanvas = function (countMarker,speed,azimut) {
        document.getElementById('markerId' + countMarker).innerHTML = '<canvas width="30" height="30"  style="position: absolute; left: 1px; top:2px" id="cnvShMarkerId' + countMarker + '"></canvas>'+
            '<canvas width="30" height="30" class="cnvMarkerIco" style="position: absolute; left: 0; top: 0" id="cnvMarkerId' + countMarker + '"></canvas>';
        this.cnv[countMarker] = document.getElementById('cnvMarkerId' + countMarker).getContext('2d');
        this.cnvSh[countMarker] = document.getElementById('cnvShMarkerId' + countMarker).getContext('2d');
        this.draw(this.cnv[countMarker],this.cnvSh[countMarker],countMarker,speed,azimut);

    };

    this.draw = function (cnv, cnvSh, countMarker,speed,azimut) {
        var timeAgo = this.difference(countMarker);
        cnv.translate(15, 15);
        cnvSh.translate(15, 15);
        if(timeAgo<=600){

            if(speed==0){
                /*
                 cnv.beginPath();
                 cnv.arc(1, 2, 7, 0, 2*Math.PI, true);
                 var gradient = cnv.createRadialGradient(0, -4, 0, 0, 0, 10);
                 gradient.addColorStop("0",'rgba(0,0,0,1)');
                 gradient.addColorStop("0.5",'rgba(0,0,0,0.5)');
                 gradient.addColorStop("1.0",'rgba(0,0,0,0.2)');
                 cnv.fillStyle = gradient;
                 cnv.fill();

                 cnv.beginPath();
                 cnv.arc(0, 0, 7, 0, 2*Math.PI, true);
                 var gradient = cnv.createRadialGradient(0, -4, 0, 0, 0, 10);
                 gradient.addColorStop("0",'rgba(255,255,255,1)');
                 gradient.addColorStop("0.5",'rgba(255,93,0,1)');

                 gradient.addColorStop("1.0",'rgba(195,93,0,1)');
                 cnv.fillStyle = gradient;
                 cnv.fill();

                 cnv.beginPath();
                 cnv.arc(0, 0, 7, 0, 2*Math.PI, true);
                 cnv.strokeStyle = '#9b4d00';
                 cnv.stroke();

                 */
                cnv.scale(0.9,0.9);
                var gradient = cnv.createRadialGradient(0, 0, 0, 0, 0, 14);
                gradient.addColorStop("0",'rgba(255,84,84,1)');
                gradient.addColorStop("1.0",'rgba(207,0,0,0.8)');
                cnv.fillStyle = gradient;
                cnv.beginPath();
                cnv.moveTo(4, 9);
                cnv.lineTo(9, 4);
                cnv.lineTo(9, -4);
                cnv.lineTo(4, -9);
                cnv.lineTo(-4, -9);
                cnv.lineTo(-9, -4);
                cnv.lineTo(-9, 4);
                cnv.lineTo(-4, 9);
                cnv.lineTo(4, 9);
                cnv.lineWidth = 1;
                cnv.strokeStyle = 'rgba(255,255,255,1)';
                cnv.stroke();
                cnv.fill();

                cnv.fillStyle = "#fff";
                cnv.strokeStyle = "#000";
                cnv.font = "bold 5px Arial";
                cnv.textBaseline = "middle";
                cnv.textAlign = "center";
                cnv.fillText("STOP", 0, 0.5);


                cnv.fillStyle = 'rgba(0,0,0,0.7)';
                cnv.strokeStyle = "#000";
                cnv.font = "bold 5px Arial";
                cnv.textBaseline = "middle";
                cnv.textAlign = "center";
                cnv.fillText("STOP", 0, 1);



                cnvSh.scale(0.9,0.9);
                var gradient = cnvSh.createRadialGradient(0, 0, 0, 0, 0, 14);
                gradient.addColorStop("0",'rgba(0,0,0,1)');
                gradient.addColorStop("1.0",'rgba(0,0,0,0.3)');
                cnvSh.fillStyle = gradient;
                cnvSh.beginPath();
                cnvSh.moveTo(4, 9);
                cnvSh.lineTo(9, 4);
                cnvSh.lineTo(9, -4);
                cnvSh.lineTo(4, -9);
                cnvSh.lineTo(-4, -9);
                cnvSh.lineTo(-9, -4);
                cnvSh.lineTo(-9, 4);
                cnvSh.lineTo(-4, 9);
                cnvSh.lineTo(4, 9);
                cnvSh.lineWidth = 1;
                cnvSh.strokeStyle = 'rgba(0,0,0,0.3)';
                cnvSh.stroke();
                cnvSh.fill();



            }else{

                cnv.rotate(this.degree(azimut));
                cnvSh.rotate(this.degree(azimut));

                cnvSh.beginPath();
                cnvSh.moveTo(-7, 9);
                cnvSh.lineTo(0, -9);
                cnvSh.lineTo(7, 9);
                cnvSh.lineTo(0, 3);
                cnvSh.lineTo(-7, 9);
                var gradient = cnvSh.createRadialGradient(0, -2, 0, 0, 0, 10);
                gradient.addColorStop("0",'rgba(0,0,0,1)');
                gradient.addColorStop("0.5",'rgba(0,0,0,0.8)');
                gradient.addColorStop("1.0",'rgba(0,0,0,0.5)');
                cnvSh.fillStyle = gradient;
                cnvSh.fill();



                cnv.beginPath();
                cnv.moveTo(-7, 9);
                cnv.lineTo(0, -9);
                cnv.lineTo(7, 9);
                cnv.lineTo(0, 3);
                cnv.lineTo(-7, 9);
                var gradient = cnv.createRadialGradient(0, -2, 0, 0, 0, 10);
                gradient.addColorStop("0",'rgba(255,255,255,1)');
                gradient.addColorStop("0.5",'rgba(0,96,255,1)');
                gradient.addColorStop("1.0",'rgba(0,0,255,1)');
                cnv.fillStyle = gradient;
                cnv.fill();




                cnv.lineWidth = 1;
                cnv.strokeStyle = '#00000';
                cnv.stroke();
                //  alert(azimut)
            }



            // alert('ds');

        }else if(timeAgo<86400){

            var gradient = cnv.createRadialGradient(0, 0, 0, 0, 0, 14);
            // Add the colors with fixed stops at 1/4 of the width.
            gradient.addColorStop("0",'rgba(95,40,255,1)');
            gradient.addColorStop("1.0",'rgba(0,72,255,0.8)');
            cnv.fillStyle = gradient;
            cnv.fillRect(-7, -7, 14, 14);

            cnv.beginPath();
            cnv.moveTo(-7, -7);
            cnv.lineTo(7, -7);
            cnv.lineTo(7, 7);
            cnv.lineTo(-7, 7);
            cnv.lineTo(-7, -7);
            cnv.lineWidth = 1;
            cnv.strokeStyle = 'rgba(255,255,255,1)';
            cnv.stroke();

            gradient = cnvSh.createRadialGradient(0, 0, 0, 0, 0, 14);
            gradient.addColorStop("0",'rgba(0,0,0,1)');
            gradient.addColorStop("1.0",'rgba(0,0,0,0.5)');



            cnvSh.beginPath();
            cnvSh.moveTo(-7, -7);
            cnvSh.lineTo(7, -7);
            cnvSh.lineTo(7, 7);
            cnvSh.lineTo(-7, 7);
            cnvSh.lineTo(-7, -7);
            cnvSh.lineWidth = 1;
            cnvSh.strokeStyle = 'rgba(0,0,0,0.5)';
            cnvSh.stroke();
            cnvSh.fillStyle = gradient;
            cnvSh.fill();

            cnv.fillStyle = "#000";
            cnv.strokeStyle = "#000";
            cnv.textBaseline = "middle";
            cnv.textAlign = "center";
            cnv.fillText("P", 1, 2);

            cnv.fillStyle = "#fff";
            cnv.strokeStyle = "#000";
            cnv.textBaseline = "middle";
            cnv.textAlign = "center";
            cnv.fillText("P", 0, 1);
            //cnv.fillText("middle", 400, 75);





            //alert('86400');
        }else{
            var gradient = cnv.createRadialGradient(0, 0, 0, 0, 0, 14);
            // Add the colors with fixed stops at 1/4 of the width.
            gradient.addColorStop("0",'rgba(120,198,250,0.6)');
            gradient.addColorStop("1.0",'rgba(145,189,220,0.2)');
            cnv.fillStyle = gradient;
            cnv.fillRect(-7, -7, 14, 14);

            cnv.beginPath();
            cnv.moveTo(-7, -7);
            cnv.lineTo(7, -7);
            cnv.lineTo(7, 7);
            cnv.lineTo(-7, 7);
            cnv.lineTo(-7, -7);
            cnv.lineWidth = 1;
            cnv.strokeStyle = 'rgba(255,255,255,0.2)';
            cnv.stroke();

            gradient = cnvSh.createRadialGradient(0, 0, 0, 0, 0, 14);
            gradient.addColorStop("0",'rgba(0,0,0,0.4)');
            gradient.addColorStop("1.0",'rgba(0,0,0,0.3)');



            cnvSh.beginPath();
            cnvSh.moveTo(-7, -7);
            cnvSh.lineTo(7, -7);
            cnvSh.lineTo(7, 7);
            cnvSh.lineTo(-7, 7);
            cnvSh.lineTo(-7, -7);
            cnvSh.lineWidth = 1;
            cnvSh.strokeStyle = 'rgba(0,0,0,0.1)';
            cnvSh.stroke();
            cnvSh.fillStyle = gradient;
            cnvSh.fill();

            cnv.fillStyle = "#000";
            cnv.strokeStyle = "#000";
            cnv.textBaseline = "middle";
            cnv.textAlign = "center";
            cnv.fillText("P", 1, 2);

            cnv.fillStyle = "#fff";
            cnv.strokeStyle = "#000";
            cnv.textBaseline = "middle";
            cnv.textAlign = "center";
            cnv.fillText("P", 0, 1);
            //cnv.fillText("middle", 400, 75);

            cnv.fillStyle = '#fff';
            cnv.fillRect(-8, 6, 26, 14);

            cnv.fillStyle = "#000";
            cnv.strokeStyle = "#000";
            cnv.textBaseline = "middle";
            cnv.textAlign = "center";
            cnv.font = "bold 8px Arial";
            cnv.fillText("Long", 3, 10);

            cnvSh.fillStyle = 'rgba(0,0,0,0.3)';
            cnvSh.fillRect(-8, 6, 26, 14);

        }

    };

    this.difference = function (i) {
        var presentSS = calcTime.ssYear(calcTime.present_time());//секунд с начала года текущие
        var parcelSS = calcTime.ssYear(calcTime.plusGmt(arrLastPos[i][3]));  //секунд с начала года в посылке
        return (presentSS - parcelSS);
    };

    this.degree = function(a) {
        return a * Math.PI / 180;
    };

}
var canvasMarker = new CanvasMarker;
